package com.crm.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.crm.entity.ModulesTreeNode;
import com.crm.entity.Roles;
import com.crm.service.ModulesService;
import com.crm.service.RolesModulesService;
import com.crm.service.RolesService;
import com.crm.utils.PageUtil;
import com.crm.utils.ResponeMessageUtil;
import com.crm.utils.UUIDUtil;

@RequestMapping("/roles")
@RestController
public class RolesController {
	
	@Autowired
	private RolesService rolesService;
	@Autowired 
	private ModulesService modulesService;
	@Autowired
	private RolesModulesService rolesModulesService;
	
	@RequestMapping("/GetRolesLayUI")
	public ResponeMessageUtil GetRolesLayUI(Integer limit,Integer page,String name) {
		Integer setValue = PageUtil.getSetValue(page, limit);
		return ResponeMessageUtil.ok(rolesService.getCount(name), rolesService.selectAll(setValue, limit));
	}
	
	/**
	* 查询所有的权限
	* @return List
	*/
	public List<ModulesTreeNode> getRolesTreeNodes(String rolesIds) {
		List<ModulesTreeNode> modulesTreeNodes = modulesService.selectAllTree(rolesIds);
		for (ModulesTreeNode modulesTreeNode : modulesTreeNodes) {
			if (modulesTreeNode.getParentid()==0) {
				modulesTreeNode.setChecked(false);
			}
		}
		return modulesTreeNodes;
	}
	
	/**
	* 生成模块类型树
	* @return List
	*/
	@RequestMapping("/GetModulesByRoleIdLayUI")
	public List<ModulesTreeNode> getRolesTreeNode(String rolesIds) {
		List<ModulesTreeNode> rolesTreeList = this.getRolesTreeNodes(rolesIds);
		List<ModulesTreeNode> rolesTreeNodes = new ArrayList<ModulesTreeNode>();
		for (ModulesTreeNode rolesTreeNode : rolesTreeList) {
			if (rolesTreeNode.getParentid()==0) {
				rolesTreeNodes.add(findChildren(rolesTreeNode, rolesTreeList));
			}
		}
		return rolesTreeNodes;
	}
	
	/**
	* 查找子类型
	* @return List
	*/
	public ModulesTreeNode findChildren(ModulesTreeNode rolesTreeNode, List<ModulesTreeNode> rolesTreeList) {
		for (ModulesTreeNode item : rolesTreeList) {
			 if (rolesTreeNode.getId()==item.getParentid()) {
				 if(rolesTreeNode.getChildren()==null) {
					 rolesTreeNode.setChildren(new ArrayList<ModulesTreeNode>());
				 } 
				 rolesTreeNode.getChildren().add(findChildren(item,rolesTreeList));
			 } 
		}
		return rolesTreeNode;
	}
	
	@RequestMapping("/SetSysRights")
	@Transactional
	public boolean SetSysRights(String parentIds,String rId) {
		//根据rId删除权限
		rolesModulesService.deleteByUidAndModuleid(rId,"");
		if (parentIds != null && !"".equals(parentIds) && parentIds != "") {//parentIds为空则视为取消全部权限
			int flag = 0;
			String[] m = parentIds.split(",");
			for (int i = 0; i < m.length; i++) {
				String id = UUIDUtil.getUUID();
				flag = rolesModulesService.insertByUidAndModuleid(id, rId, m[i]);
			}
			if (flag<=0) {
				return false;
			}
		}
		return true;
	}
	
	@RequestMapping("/CreateRole")
	public boolean insertRoles(Roles roles) {
		roles.setId(UUIDUtil.getUUID());
		return rolesService.insert(roles)>0 ? true:false;
	}
	
	@RequestMapping("/DeleteRole")
	public boolean  deleteRoles(String id) {
		return rolesService.deleteByPrimaryKey(id)>0 ? true:false;
	}
	
	@RequestMapping("/UpdateRole")
	public boolean  updateRoles(Roles roles) {
		return rolesService.update(roles)>0 ? true:false;
	}
	
	/**
	 * 删除角色权限
	 * @param roleid
	 * @param moduleid
	 * @return
	 */
	@RequestMapping("/RemoveRoleFromModule")
	public boolean  RemoveRoleFromRole(String roleid,String moduleid) {
		return rolesModulesService.deleteByUidAndModuleid(roleid,moduleid)>0?true:false;
	} 
	
	/**
	 * 增加角色权限
	 * @param roleid
	 * @param moduleid
	 * @return
	 */
	@RequestMapping("/AddRoleToModule")
	public boolean  AddRoleToRole(String roleid,String moduleid) {
		String id = UUIDUtil.getUUID();
		return rolesModulesService.insertByUidAndModuleid(id,roleid,moduleid)>0?true:false;
	}
	
	@RequestMapping("/GetRolesAll")
	public List<Roles> GetRolesAll() {
		return rolesService.selectRolesAll();
	}
	
	@RequestMapping("/getRolesByUid")
	public Map<String, Object> getRolesByUid(String id) {
		Map<String, Object> map = new HashMap<>();
		List<Roles> rolList = rolesService.getRolesByUid(id);
		map.put("code", "0");
		map.put("msg", "success");
		map.put("count", rolList.size());
		map.put("data", rolList);
		return map;
	}
	
}